package com.leo.oplayer.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



/**
 * @explanation
 */
public class DateUtil {

    /**
     * @time 5:51:36 PM
     * @param time
     * @param fromFormat
     * @param toFormat
     * @return
     * @throws ParseException
     * @throws TelwareException
     * @explanation change a date string from the fromFormat to toFormat
     */
    public static String formatDate(String time, String fromFormat,
            String toFormat) throws ParseException {
    	if (time == null || "".equals(time.trim()))
    		return null;
        String dateTime = time;

        SimpleDateFormat oldFormat = new SimpleDateFormat(fromFormat);
        SimpleDateFormat newFormat = new SimpleDateFormat(toFormat);

        String dataStr = null;

        dataStr = newFormat.format(oldFormat.parse(dateTime));

        return dataStr;
    }
    
    public static String formatDate(String time,String toFormat){
    	if (time == null || "".equals(time.trim()))
    		return "";
    	
    	String fromFormat = null;
    	if(time.length() == 6){
    		fromFormat = "HHmmss";
    	}else if(time.length()==8){
    		fromFormat = "yyyyMMdd";
    	}else if(time.length() == 14){
    		fromFormat = "yyyyMMddHHmmss";
    	}else{
    		return time;
    	}
    	SimpleDateFormat oldFormat = new SimpleDateFormat(fromFormat);
        SimpleDateFormat newFormat = new SimpleDateFormat(toFormat);
        try {
			return newFormat.format(oldFormat.parse(time));
		} catch (ParseException e) {			
			return time;
		}
    }

    /**
     * @time 10:43:46 AM
     * @return
     * @explanation get current date string meeting formatStr
     */
    public static String getCurrentDate(String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(new Date());
    }

    /**
     * @time 10:43:46 AM
     * @return
     * @explanation format date according to toFormat
     */
    public static String formatDate(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }

 
    /**
     * @param formater 为null或为空时，设置为yyyyMMddHHmmss
     * @return
     */
    public static String getCurrentTime(String formater) {
        if ("".equals(formater)) {
            formater = "yyyyMMddHHmmss";
        }
        return DateUtil.getCurrentDate("yyyyMMddHHmmss");
    }
    
    /**
     * 获取当前月份
     * @return 月份，如:201001
     */
    public static String getCurrentMonth(){
   	 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String day =  format.format(new Date());
        return day.substring(0, 6);
   }
    
    /**
     * 把long型转化为日期时间
     * @param date
     * @param formatStr
     * @return
     */
    public static String getStringDate(long date, String formatStr){
    	Date temp = new Date(date);
    	SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(temp);
    }
    
    /**
     * 检查参数日期+spanDay 是否小于于今天
     * @param yyyyMMdd
     * @param spanDay
     * @return
     */
    public static boolean overMaxSpan(String yyyyMMdd,int spanDay){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -spanDay);
		int val = Integer.valueOf(formatDate(calendar.getTime(),"yyyyMMdd"));
		int paramVal = Integer.valueOf(yyyyMMdd);
		return (val>paramVal);
	}

}
